package com.gof.example.mybatistest;

import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * Created by zhiyuan on 2018/4/25 16:33.
 * haiming.fei@tongdun.cn
 */
public class MyPropTokenizer implements Iterator<MyPropTokenizer> {

    private String name;
    private String indexedName;
    private String index;
    private String children;

    public MyPropTokenizer(String fullname) {
        int delim = fullname.indexOf(46);
        if (delim > -1) {
            this.name = fullname.substring(0, delim);
            this.children = fullname.substring(delim + 1);
        } else {
            this.name = fullname;
            this.children = null;
        }

        this.indexedName = this.name;
        delim = this.name.indexOf(91);
        if (delim > -1) {
            this.index = this.name.substring(delim + 1, this.name.length() - 1);
            this.name = this.name.substring(0, delim);
        }

    }

    @Override
    public String toString() {
        return "MyPropTokenizer{" +
                "name='" + name + '\'' +
                ", indexedName='" + indexedName + '\'' +
                ", index='" + index + '\'' +
                ", children='" + children + '\'' +
                '}';
    }

    public String getName() {
        return this.name;
    }

    public String getIndex() {
        return this.index;
    }

    public String getIndexedName() {
        return this.indexedName;
    }

    public String getChildren() {
        return this.children;
    }

    public boolean hasNext() {
        return this.children != null;
    }

    public MyPropTokenizer next() {
        return new MyPropTokenizer(this.children);
    }

    public void remove() {
        throw new UnsupportedOperationException("Remove is not supported, as it has no meaning in the context of properties.");
    }

    public Iterator<MyPropTokenizer> iterator() {
        return this;
    }
}
